package com.semidata.trp.report.report;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.ibatis.session.SqlSession;

import com.semidata.trp.report.config.Config;
import com.semidata.trp.report.mapper.NRTReportMapper;
import com.semidata.trp.report.utils.SessionUtils;

public class MakeTestLog {
	private static List<String> positionMedia = new ArrayList<String>();
	private static List<Integer> orgList = new ArrayList<Integer>();
	private static List<Integer> straList = new ArrayList<Integer>();
	private static List<Integer> resList = new ArrayList<Integer>();
	List<String> machineList = new ArrayList<String>();
	private static Config config = Config.getConfig();
	private static String[] headList = { "+", "-", "0", "1", "2", "3", "4",
			"5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
			"V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h",
			"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
			"v", "w", "x", "y", "z" };

	public void setAllList(String date) {
		List<Map<String, Integer>> posMedia = new ArrayList<Map<String, Integer>>();
		SqlSession session = SessionUtils.getSession();
		/*
		 * orgList = new ArrayList<Integer>(); straList = new
		 * ArrayList<Integer>(); resList = new ArrayList<Integer>(); machineList
		 * = new ArrayList<String>();
		 */
		positionMedia.clear();
		;
		try {
			NRTReportMapper mapper = session.getMapper(NRTReportMapper.class);
			posMedia = mapper.getAllMediaByPosition();
			orgList = mapper.getAllOrgId();
			straList = mapper.getAllStraId(date);
			resList = mapper.getAllResourceId(date);
			machineList = mapper.getMonitorServerList();
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			session.close();
		}
		for (Map<String, Integer> posMediaIt : posMedia) {
			// String id = String.valueOf(posMediaIt.get("id"));
			// String media_id = String.valueOf(posMediaIt.get("media_id"));
			// System.out.println(id);
			// System.out.println(media_id);
			positionMedia.add(String.valueOf(posMediaIt.get("id")));
		}
		posMedia = null;
	}

	public void makeLogs(String date,int a) {
		setAllList(date);
		String rootDir = config.getMonitorLogDir();
		File file = new File(rootDir);
		if (!file.exists()) {
			file.mkdirs();
		}
		StringBuffer sb = new StringBuffer(rootDir);
		sb.append("/");
		sb.append(date);
		file = new File(sb.toString());
		if (!file.exists()) {
			file.mkdirs();
		}
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmm");
		Calendar c = new GregorianCalendar();
		c.set(Integer.valueOf(date.substring(0, 4)),
				Integer.valueOf(date.substring(4, 6)) - 1,
				Integer.valueOf(date.substring(6)), 0, 0);
		String logName;
		FileWriter fw = null;
		Random rd = new Random();
		int sSize = straList.size(), oSize = orgList.size(), pSize = positionMedia
				.size(), rSize = resList.size();
		int tag;
		for (int i = 0; i < 1440; i++) {
			logName = dateformat.format(c.getTime());
			sb.setLength(0);
			sb.append(file.getAbsolutePath());
			sb.append("/");
			sb.append(logName);
			sb.append(".local.log");
			try {
				fw = new FileWriter(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int j = 0; j < a; j++) {
				sb.setLength(0);
				sb.append("/a.gif?i=10.100.8.126&m=1&t=1234&b=5.3&c=12300&a=");
				sb.append(orgList.get(rd.nextInt(oSize)));
				sb.append("&p=");
				sb.append(positionMedia.get(rd.nextInt(pSize)));
				sb.append("&r=");
				sb.append(resList.get(rd.nextInt(rSize)));
				sb.append("&s=");
				sb.append(straList.get(rd.nextInt(sSize)));
				sb.append("&u=");
				for (int k = 0; k < 7; k++) {
					tag = rd.nextInt(64);
					sb.append(headList[tag]);
				}
				sb.append("&mt=");
				sb.append(rd.nextInt(3));
				sb.append("&k1=");
				sb.append(headList[rd.nextInt(62) + 2]);
				sb.append("&k2=");
				sb.append(headList[rd.nextInt(62) + 2]);
				sb.append("&k3=");
				sb.append(headList[rd.nextInt(62) + 2]);
				sb.append("\n");
				try {
					fw.write(sb.toString());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				/*
				 * if (result.get("a") == null || result.get("p") == null ||
				 * result.get("r") == null || result.get("s") == null ||
				 * result.get("m") == null || result.get("t") == null ||
				 * result.get("b") == null || result.get("i") == null ||
				 * result.get("u") == null || result.get("mt") == null)
				 * 
				 * 
				 * ((straList.indexOf(Integer.valueOf(result.get("s"))) == -1)
				 * || (orgList.indexOf(Integer.valueOf(result.get("a"))) == -1)
				 * || (positionMedia.get(result.get("p")) == null) ||
				 * (resList.indexOf(Integer.valueOf(result.get("r"))) == -1))
				 */
			}
			try {
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1);
		}
	}

	public static void main(String[] args) {
		MakeTestLog test = new MakeTestLog();
		test.setAllList("20140910");
		System.out.println(test.positionMedia.size());
		Random rd = new Random();
		int size = test.positionMedia.size();
		System.out.println(test.positionMedia.get(rd.nextInt(size)));
	}

}
